----------------------------------------------------------------------------------------------------------------------------------
--[[



]]--
----------------------------------------------------------------------------------------------------------------------------------
--- 获取电池
    local battery_com = nil
    local function GetBatteryCom()
        if battery_com == nil then
            battery_com = require("components/loramia_com_power_battery")
        end
        return battery_com
    end
    local function GetAllBatteries()
        local com = GetBatteryCom()
        if com then
            return com.GetAllBatteries()
        end
        return {}
    end
----------------------------------------------------------------------------------------------------------------------------------
--- replica
    local function GetReplica(self)
        return self.inst.replica.loramia_com_power_device or self.inst.replica._.loramia_com_power_device
    end
    local function display_indicator_radius(self, radius)
        local replica_com = GetReplica(self)
        if replica_com then
            replica_com:SetIndicatorRadius(radius)
        end
    end
    local function display_indicator_fn(self, display)
        local replica_com = GetReplica(self)
        if replica_com then
            replica_com:SetIndicator(display)
        end
    end
----------------------------------------------------------------------------------------------------------------------------------
local loramia_com_power_device = Class(function(self, inst)
    self.inst = inst
    inst:AddTag("loramia_com_power_device")
    
    ----------------------------------------------------------
    --- 单个定时任务
        self.periodic_time = 1
        self.periodic_succeed_fn = nil
        self.periodic_task = nil
        self.periodic_cost = nil
        self.periodic_test_fn = nil
    ----------------------------------------------------------
    --- 指示器
        self.display_indicator = false
        self.display_indicator_radius = 1
    ----------------------------------------------------------

end,
nil,
{
    display_indicator = display_indicator_fn,
    display_indicator_radius = display_indicator_radius,
})
------------------------------------------------------------------------------------------------------------------------------
--- 指示器
    function loramia_com_power_device:SetIndicator(flag)
        self.display_indicator = flag
    end
    function loramia_com_power_device:SetIndicatorRadius(value)
        self.display_indicator_radius = value
    end
------------------------------------------------------------------------------------------------------------------------------
--- 电池扫描
    function loramia_com_power_device:GetAllBatteries()
        return GetAllBatteries()
    end
    function loramia_com_power_device:GetAllLinkedBatteries()
        local batteries = self:GetAllBatteries()
        -- local x,y,z = self.inst.Transform:GetWorldPosition()
        local ret = {}
        for _,battery in ipairs(batteries) do
            if battery then
                local radius = battery.components.loramia_com_power_battery:GetRadius()
                local radius_sq = radius*radius
                if self.inst:GetDistanceSqToInst(battery) <= radius_sq and not battery.components.loramia_com_power_battery.ignore_all_devices then
                    table.insert(ret, battery)
                end
            end
        end
        return ret
    end
------------------------------------------------------------------------------------------------------------------------------
--- 成功获取电量.优先消耗最近的节点。
    function loramia_com_power_device:IsGotPowerSucceed(value)
        value = math.max(0,value or 0) -- 防止负数
        local batteries = self:GetAllLinkedBatteries()
        if #batteries == 0 then
            return false
        end        
        --- 按距离排序
        if #batteries > 1 then
            table.sort(batteries, function(a,b)
                return self.inst:GetDistanceSqToInst(a) < self.inst:GetDistanceSqToInst(b)
            end)
        end
        for _,battery in ipairs(batteries) do
            if battery.components.loramia_com_power_battery:Has(value) then
                battery.components.loramia_com_power_battery:DoDelta(-value)
                return true
            end
        end
        return false
    end
    function loramia_com_power_device:CanTakePower(value)
        value = math.max(0,value or 0) -- 防止负数
        local batteries = self:GetAllLinkedBatteries()
        if #batteries == 0 then
            return false
        end
        for _,battery in ipairs(batteries) do
            if battery.components.loramia_com_power_battery:Has(value) then
                return true
            end
        end
        return false
    end
    function loramia_com_power_device:GetMaxCanTakePower() -- 获取范围内最大的电量值
        local batteries = self:GetAllLinkedBatteries()
        if #batteries == 0 then
            return 0
        end
        local max = 0
        for _,battery in ipairs(batteries) do
            max = math.max(max,battery.components.loramia_com_power_battery:GetCurrent())
        end
        return max
    end
------------------------------------------------------------------------------------------------------------------------------
--- 单个启动定时任务
    function loramia_com_power_device:SetPeriodicTime(time)
        self.periodic_time = time
    end
    function loramia_com_power_device:SetPeriodicSucceedFn(fn)
        self.periodic_succeed_fn = fn
    end
    function loramia_com_power_device:SetPeriodicTestFn(fn)
        self.periodic_test_fn = fn
    end
    function loramia_com_power_device:SetPeriodicCost(value)
        self.periodic_cost = value
    end
    function loramia_com_power_device:StartPeriodicTask()
        self.inst:DoTaskInTime(0,function()            
            if not (self.periodic_time and self.periodic_succeed_fn and self.periodic_cost)  then
                return
            end
            if self.periodic_task then
                self.periodic_task:Cancel()
            end
            self.periodic_task = self.inst:DoPeriodicTask(self.periodic_time,function()
                if (self.periodic_test_fn == nil or self.periodic_test_fn(self.inst) ) and self:IsGotPowerSucceed(self.periodic_cost) then
                    self.periodic_succeed_fn(self.inst)
                end
            end)
        end)
    end        
------------------------------------------------------------------------------------------------------------------------------
return loramia_com_power_device







